﻿@{
ViewBag.Title = "柯乐义";
}
<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<div>
<h3>
    提交留言</h3>
@using (Html.BeginForm("Create", "Message", FormMethod.Post, new { @class = "keleyi" }))
{
<table>
    <tr>
        <td>
            标题：
        </td>
        <td>@Html.TextBox("kTitle")
        </td>
    </tr>
    <tr>
        <td>
            Email：
        </td>
        <td>@Html.TextBox("kEmail") 例如：zhangsan@@keleyi.com
        </td>
    </tr>
    <tr>
        <td>
            QQ：
        </td>
        <td>@Html.TextBox("kQQ")
        </td>
    </tr>
    <tr>
        <td>
            内容：
        </td>
        <td>@Html.TextArea("kcontent", new { rows = 10, cols = 50 })
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <button type="submit" id="tijiao">提交留言</button> <a href="/">返回</a>
            <span id="indicator" style="display: none"><img src="/content/load.gif"
                    alt="loading..." /></span>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            本站会选择一些留言在留言板公开。
        </td>
    </tr>
</table>
}
</div>
<script type="text/javascript">
$(document).ready(function () {
    $("form.keleyi").submit(function (event) {
        event.preventDefault();

        if (CheckMail($("#kEmail").val()) == false) {
            alert("未填写邮箱或者邮箱格式错误");
            return;
        }
        if ($("#kcontent").val() == '') {
            alert('请输入内容。');
            return;
        }

        keleyiAjax(this, update_sessions, "html");
    });
});

function keleyiAjax(form, callback, format) {
    $("#indicator").show();
    $.ajax({
        url: form.action,
        type: form.method,
        dataType: format,
        data: $(form).serialize(),
        completed: $("#indicator").hide(),
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
        },
        success: callback
    });
}

function update_sessions(result) {
    if (result == "") {
        $("form.keleyi")[0].reset();
        alert("感谢您的留言!");
        window.location = "/";
    }
    else
        alert(result);
}

function CheckMail(mail) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(mail)) return true;
    else {
        return false;
    }
}
</script>